//
//  XJDiscoverSearchView.swift
//  XJWeiBO
//
//  Created by 452608533 on 16/7/23.
//  Copyright © 2016年 452608533. All rights reserved.
//

import UIKit

class XJDiscoverSearchView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENH, height: 35))
       
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
 }
    
    func didClick() {
        self.tex.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(self)
        }
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.layoutIfNeeded()
            }) { (_) -> Void in
              self.tex.resignFirstResponder()
        }
    }
    
    private func setupUI() {
        
        backgroundColor = RandomColor()
        addSubview(button)
        addSubview(tex)
        
        tex.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
            
        }
        
        button.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.top.bottom.trailing.equalTo(self)
        }
        
    }
    
    private lazy var tex: UITextField = {
        let img = UIImageView(imgName: "searchbar_textfield_search_icon")
        img.contentMode = .Center
        img.frame.size = CGSize(width: 35, height: 35)
       let txt = UITextField()
        txt.placeholder = "上海传智三期"
        txt.layer.borderColor = UIColor.orangeColor().CGColor
        txt.layer.borderWidth = 2
        txt.backgroundColor = UIColor.whiteColor()
        txt.leftView = img
        txt.leftViewMode = .Always
        txt.delegate = self
        return txt
    }()
    
    private lazy var button: UIButton = {
        let button = UIButton()
        button.setTitle("取消", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font =  UIFont.systemFontOfSize(15)
        button.addTarget(self, action: "didClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()


}

extension XJDiscoverSearchView: UITextFieldDelegate{
    func textFieldDidBeginEditing(textField: UITextField) {
     
        self.tex.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(self).offset(-50)
        }
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
}


















